﻿
namespace TestDeal
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Data.Linq;
    using System.Data.Linq.Mapping;
    using System.Data.SqlClient;
    using System.Text;
    using System.Net;
    using System.IO;
    using System.Threading;

    using System.Windows.Forms;

    public class DealseaSpider : DealSpider
    {
        private string htmlDoc;

        public override bool GetDealInfo()
        {
            //
            // Retrieve the deal info.
            //

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.dealsea.com/");

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode != HttpStatusCode.OK)
            {
                return false;
            }

            /*
            string tempFileName = Guid.NewGuid().ToString()+ ".tmp";
            Stream input = response.GetResponseStream();
            Stream output = new FileStream(tempFileName, FileMode.Create);

            int bufferLength = 255;
            byte[] buffer = new byte[bufferLength];
            int index = 0;
            int byteReads = 0;

            while ((byteReads = input.Read(buffer, index, bufferLength)) != 0)
            {
                output.Write(buffer, index, byteReads);
                index += byteReads;
            }
            */

            htmlDoc = new StreamReader(response.GetResponseStream()).ReadToEnd();
            //Console.WriteLine(doc);

            doParsing(htmlDoc);

            return true;
        }

        public void doParsing(string html)
        {
            Thread t = new Thread(parseMain);
            t.SetApartmentState(ApartmentState.STA);
            t.Start((object)html);
            t.Join();
        }

        private void parseMain(object html)
        {
            WebBrowser wbc = new WebBrowser();
            wbc.Navigate("about:blank");
            wbc.Document.OpenNew(false);
            //wbc.Document.Write("<html><body><div class=dealbox>abcd</div></body></html>");
            wbc.Document.Write(htmlDoc);


            HtmlDocument doc = wbc.Document;
            //doc.Write(string.Empty);

            //wbc.Stop();

            //wbc.DocumentText = "<html><body><div class=\"dealbox\">abcd</div></body></html>";


            
            HtmlElementCollection collection = doc.GetElementsByTagName("div");

            foreach (HtmlElement elem in collection)
            {
                String nameStr = elem.GetAttribute("className");
                if (nameStr == "dealbox")
                {
                    //Console.WriteLine(elem.InnerHtml);
                    //Console.WriteLine("********************************************");

                    
            WebBrowser inner = new WebBrowser();
            inner.Navigate("about:blank");
            inner.Document.OpenNew(false);
                     inner.Document.Write(elem.InnerHtml);

                    HtmlDocument innerDoc = inner.Document;
                    HtmlElementCollection dealTitles = innerDoc.GetElementsByTagName("A");

                    foreach (HtmlElement title in dealTitles)
                    {
                        string output = title.InnerHtml;

                        if (!output.StartsWith("<IMG") && output.Length > 20)
                        {
                            Console.WriteLine(title.InnerHtml);
                            Console.WriteLine("********************************************");
                        }
                    }
                    
                }
            }
            

            return;
        } 

    }
}
